using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

using common = Microsoft.ConnectionEngine.Common;
using Microsoft.ConnectionEngine.Registers.Core.ServiceProvider.Common;
using business = Microsoft.ConnectionEngine.Registers.Core.ServiceProvider.Business;

using Microsoft.ConnectionEngine.Common.Security;

namespace Microsoft.ConnectionEngine.Registers.Core.ServiceProvider.Facade
{
    class PoolManager
    {
        private PoolManager() { }

        //PoolSearchRequest 1024
         public static void PoolSearch(XmlDocument xmlMessage, out XmlDocument resultXmlMessage, out int resultMsgType)
        {
            PoolSearchRequest request = new PoolSearchRequest();
            Pools pools = new Pools();

            XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlMessage.NameTable);
            nsmgr.AddNamespace("pnp", xmlMessage.FirstChild.NamespaceURI);

            XmlNode userCredentials = xmlMessage.SelectSingleNode("//pnp:UserCredentials", nsmgr);
            //HSDPrincipal currentUser = Utilities.CreateUser(userCredentials, nsmgr);
            Utilities.CreateUser(userCredentials, nsmgr);

            XmlNode searchNode = xmlMessage.SelectSingleNode("//pnp:PoolSearchRequest", nsmgr);
            request = (PoolSearchRequest)common.Utilities.DeserializeObject(searchNode.OuterXml, typeof(PoolSearchRequest));

            pools = business.PoolManager.PoolSearch(request);

            resultMsgType = (int)MessageTypes.PoolSearchResult;
            resultXmlMessage = new XmlDocument();
            resultXmlMessage.LoadXml(common.Utilities.SerializeObject(pools, typeof(Pools)));
        }

        //PoolSelectItemRequest 1066
			public static void PoolSelectItem(XmlDocument xmlMessage, out XmlDocument resultXmlMessage, out int resultMsgType)
			{
				Pool pool = new Pool();

				XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlMessage.NameTable);
				nsmgr.AddNamespace("pnp", xmlMessage.FirstChild.NamespaceURI);

				XmlNode userCredentials = xmlMessage.SelectSingleNode("//pnp:UserCredentials", nsmgr);
				//HSDPrincipal currentUser = Utilities.CreateUser(userCredentials, nsmgr);
                Utilities.CreateUser(userCredentials, nsmgr);   

				String poolID = xmlMessage.SelectSingleNode("//pnp:PoolID", nsmgr).InnerText;
				pool = business.PoolManager.PoolSelectItem(poolID, false);  
											
				resultMsgType = (int)MessageTypes.PoolSelectItemResult;
				resultXmlMessage = new XmlDocument();
				resultXmlMessage.LoadXml(common.Utilities.SerializeObject(pool, typeof(Pool)));
			}

            //PoolCheckIn 1017
			public static void PoolUnlock(XmlDocument xmlMessage, out XmlDocument resultXmlMessage, out int resultMsgType)
			{
				XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlMessage.NameTable);
				nsmgr.AddNamespace("pnp", xmlMessage.FirstChild.NamespaceURI);

				XmlNode userCredentials = xmlMessage.SelectSingleNode("//pnp:UserCredentials", nsmgr);
				HSDPrincipal currentUser = Utilities.CreateUser(userCredentials, nsmgr);   

				String poolID = xmlMessage.SelectSingleNode("//pnp:PoolID", nsmgr).InnerText;
				//String status = business.PoolManager.PoolUnlock(poolID, currentUser.HSDIdentity.UserId);
                business.PoolManager.PoolUnlock(poolID, currentUser.HSDIdentity.UserId);  
											
				resultMsgType = 0;
				resultXmlMessage = new XmlDocument();				
			}

            //PoolCheckInAll 1038
			public static void PoolUnlockAll(XmlDocument xmlMessage, out XmlDocument resultXmlMessage, out int resultMsgType)
			{				

				XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlMessage.NameTable);
				nsmgr.AddNamespace("pnp", xmlMessage.FirstChild.NamespaceURI);

				XmlNode userCredentials = xmlMessage.SelectSingleNode("//pnp:UserCredentials", nsmgr);
				HSDPrincipal currentUser = Utilities.CreateUser(userCredentials, nsmgr);   

				//string status = business.PoolManager.PoolUnlockAll(currentUser.HSDIdentity.UserId);
                string status = business.PoolManager.PoolUnlockAll(currentUser.HSDIdentity.UserId);
			
				resultMsgType = 0;
				resultXmlMessage = new XmlDocument();				
			}

            //PoolCheckOut 1018
			public static void PoolLock(XmlDocument xmlMessage, out XmlDocument resultXmlMessage, out int resultMsgType)
			{
				Pool pool = new Pool();

				XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlMessage.NameTable);
				nsmgr.AddNamespace("pnp", xmlMessage.FirstChild.NamespaceURI);

				XmlNode userCredentials = xmlMessage.SelectSingleNode("//pnp:UserCredentials", nsmgr);
				HSDPrincipal currentUser = Utilities.CreateUser(userCredentials, nsmgr);   

				String poolID = xmlMessage.SelectSingleNode("//pnp:PoolID", nsmgr).InnerText;
				//String status = business.PoolManager.PoolLock(poolID, currentUser.HSDIdentity.UserId);
                business.PoolManager.PoolLock(poolID, currentUser.HSDIdentity.UserId);

                pool = business.PoolManager.PoolSelectItem(poolID, false);				
			
				resultMsgType = (int)MessageTypes.PoolCheckOutResult;
				resultXmlMessage = new XmlDocument();
				resultXmlMessage.LoadXml(common.Utilities.SerializeObject(pool, typeof(Pool)));
			}

            //PoolInsert 1020
			public static void PoolInsert(XmlDocument xmlMessage, out XmlDocument resultXmlMessage, out int resultMsgType)
			{
				Pool pool = new Pool();

				XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlMessage.NameTable);
				nsmgr.AddNamespace("pnp", xmlMessage.FirstChild.NamespaceURI);

				XmlNode userCredentials = xmlMessage.SelectSingleNode("//pnp:UserCredentials", nsmgr);
				HSDPrincipal currentUser = Utilities.CreateUser(userCredentials, nsmgr);   

				XmlNode poolNode = xmlMessage.SelectSingleNode("//pnp:Pool", nsmgr);
                pool = (Pool)common.Utilities.DeserializeObject(poolNode.OuterXml, typeof(Pool));

                //String status = business.PoolManager.PoolInsert(pool, currentUser.HSDIdentity.UserId);
                business.PoolManager.PoolInsert(pool, currentUser.HSDIdentity.UserId);
                //if (status == "0")
                //    pool = business.PoolManager.PoolSelectItem(pool.PoolID.ToString());				
			
				resultMsgType = (int)MessageTypes.PoolInsertResult;
				resultXmlMessage = new XmlDocument();
				resultXmlMessage.LoadXml(common.Utilities.SerializeObject(pool, typeof(Pool)));
			}

            //PoolUpdate 1019
			public static void PoolUpdate(XmlDocument xmlMessage, out XmlDocument resultXmlMessage, out int resultMsgType)
			{
				Pool pool = new Pool();

				XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlMessage.NameTable);
				nsmgr.AddNamespace("pnp", xmlMessage.FirstChild.NamespaceURI);

				XmlNode userCredentials = xmlMessage.SelectSingleNode("//pnp:UserCredentials", nsmgr);
				HSDPrincipal currentUser = Utilities.CreateUser(userCredentials, nsmgr);   

				XmlNode poolNode = xmlMessage.SelectSingleNode("//pnp:Pool", nsmgr);
                pool = (Pool)common.Utilities.DeserializeObject(poolNode.OuterXml, typeof(Pool));

                //String status = business.PoolManager.PoolUpdate(pool, currentUser.HSDIdentity.UserId);
                business.PoolManager.PoolUpdate(pool, currentUser.HSDIdentity.UserId);
                pool = business.PoolManager.PoolSelectItem(pool.PoolID.ToString());

                resultMsgType = (int)MessageTypes.PoolUpdateResult;
                resultXmlMessage = new XmlDocument();
                resultXmlMessage.LoadXml(common.Utilities.SerializeObject(pool, typeof(Pool)));
			}

            //PoolDelete 1016
			public static void PoolDelete(XmlDocument xmlMessage, out XmlDocument resultXmlMessage, out int resultMsgType)
			{			
				XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlMessage.NameTable);
				nsmgr.AddNamespace("pnp", xmlMessage.FirstChild.NamespaceURI);

				XmlNode userCredentials = xmlMessage.SelectSingleNode("//pnp:UserCredentials", nsmgr);
				HSDPrincipal currentUser = Utilities.CreateUser(userCredentials, nsmgr);   

				String poolID = xmlMessage.SelectSingleNode("//pnp:PoolID", nsmgr).InnerText;
				//String status = business.PoolManager.PoolDelete(poolID, currentUser.HSDIdentity.UserId);
                business.PoolManager.PoolDelete(poolID, currentUser.HSDIdentity.UserId);
			
				resultMsgType = 0;
				resultXmlMessage = new XmlDocument();
            }
     
    }
}

